package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.dubbo.mapper.OmsOrderOperateHistoryMapper;
import com.mall.dubbo.mapper.UmsRoleMapper;
import com.mall.model.domain.OmsOrderOperateHistory;
import com.mall.model.domain.UmsRole;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author xyiaao
 * @description 针对表【oms_order_operate_history(订单操作历史记录)】的数据库操作Api实现
 * @createDate 2022-03-29 19:46:09
 */
@DubboService
public class OmsOrderOperateHistoryApiImpl extends ServiceImpl<OmsOrderOperateHistoryMapper, OmsOrderOperateHistory>
        implements OmsOrderOperateHistoryApi {

    @Autowired
    private UmsRoleMapper umsRoleMapper;


    @Autowired
    private OmsOrderOperateHistoryMapper omsOrderOperateHistoryMapper;


    @Override
    public List<OmsOrderOperateHistory> listByOrderId(Long orderId) {

        LambdaQueryWrapper<OmsOrderOperateHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsOrderOperateHistory::getOrderId, orderId);
        return omsOrderOperateHistoryMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteByOrderIds(List<Long> ids) {

        LambdaQueryWrapper<OmsOrderOperateHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(OmsOrderOperateHistory::getOrderId, ids);
        omsOrderOperateHistoryMapper.delete(queryWrapper);
    }


    /**
     * 记录订单操作记录
     *
     * @param userId
     * @param ids
     * @param status
     * @param note
     */
    @Override
    public void insertOmsOrderOperateHistory(Long userId, List<Long> ids, int status, String note) {
        // 查出角色信息
        UmsRole umsRole = umsRoleMapper.findUmsRoleByUmsAdminId(userId);
        for (Long orderId : ids) {
            insertOmsOrderOperateHistory(status, note, umsRole, orderId);
        }
    }

    @Override
    public void insertOmsOrderOperateHistory(Long userId, Long orderId, int status, String note) {
        // 查出角色信息
        UmsRole umsRole = umsRoleMapper.findUmsRoleByUmsAdminId(userId);
        insertOmsOrderOperateHistory(status, note, umsRole, orderId);
    }

    private void insertOmsOrderOperateHistory(int status, String note, UmsRole umsRole, Long orderId) {

        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setCreateTime(new Date());
        history.setNote(note);
        history.setOrderStatus(status);
        history.setOrderId(orderId);
        history.setOperateMan(umsRole.getName());
        omsOrderOperateHistoryMapper.insert(history);
    }

}




